package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void insertOne(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setDisciplineTimes(0);
        student.setDisciplineScore(0);
        studentMapper.insertOne(student);

    }

    @Override
    public void deleteOne(Integer id) {
        studentMapper.deleteOne(id);

    }

    @Override
    public void updateOne(Student student) {
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.updateOne(student);


    }

    @Override
    public Student selectOne(Integer id) {
        Student student = studentMapper.selectOne(id);

        return student;
    }

    @Override
    public PageBean<Student> selectPage(Integer page,
                                        Integer pageSize,
                                        String name,
                                        String highestDegree,
                                        String classesId,
                                        String studentNumber) {
        //分页插件引入
        PageHelper.startPage(page,pageSize);
        //分页查询返回
        Page<Student> studentPage = studentMapper.selectPage(
                name,highestDegree, classesId, studentNumber);

        //定义新对象接收返回值
        PageBean<Student> studentPageBean = new PageBean<>();

        //接收数据
        studentPageBean.setTotal(studentPage.getTotal());
        studentPageBean.setRows(studentPage.getResult());

        return studentPageBean;
    }

    /**
     * 修改学生违纪分数
     * @param student
     */
    @Override
    public void updateDscore(Student student) {
        studentMapper.updateDscore(student);

    }




}
